package com.cash.loan.modular.cash.controller;

import com.cash.loan.core.upload.QiniuService;
import com.cash.loan.core.util.TextValidator;
import com.cash.loan.modular.cash.result.PageResult;
import com.cash.loan.modular.cash.result.ServerResponse;
import com.cash.loan.modular.cash.service.AdService;
import com.cash.loan.modular.cash.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 轮播管理
 *
 * @author Administrator
 * @Date 2018-07-11 11:50
 */
@Controller
@RequestMapping(value = "/cash/ad")
@Api(description = "广告轮播管理")
public class AdController {

    private static final String PREFIX = "/cash/ad";

    @Autowired
    private AdService adService;

    @Autowired
    private ProductService productService;

    @Autowired
    private QiniuService qiniuService;

    /**
     * 进入列表页
     * @return
     */
    @RequestMapping(value = "/listPage")
    public String toAdListPage() {
        return PREFIX + "/picture_list.html";
    }

    /**
     * 新增页面
     * @return
     */
    @RequestMapping(value = "/createPage")
    public String toCreatePage() {
        return PREFIX + "/picture_add.html";
    }

    /**
     * 修改页面
     * @return
     */
    @RequestMapping(value = "/editPage")
    public String toEditPage() {
        return PREFIX + "/picture_edit.html";
    }

    @ApiOperation("轮播图列表")
    @GetMapping(value = "/list")
    @ResponseBody
    public ServerResponse adList(@RequestParam(value = "currentPage",required = false,defaultValue = "1") Integer currentPage,
                                 @RequestParam(value = "pageSize",required = false,defaultValue = "20") Integer pageSize,
                                 @RequestParam(value = "keyword",required = false) String keyword,
                                 @RequestParam(value = "cateId",required = false) Integer cateId) {

        PageResult<Map<String,Object>> pageResult = adService.adList(currentPage,pageSize,keyword,cateId);

        return ServerResponse.getSuccess("success",pageResult);
    }

    @ApiOperation("一个轮播的详情")
    @GetMapping(value = "/detail")
    @ResponseBody
    public ServerResponse getOneProductAd(@RequestParam(value = "id") Long id) {
        Map<String,Object> map = adService.getOneById(id);
        return ServerResponse.getSuccess("success",map);
    }

    @ApiOperation("广告页产品列表查询")
    @GetMapping(value = "/productList")
    @ResponseBody
    public ServerResponse getProductListForAd(@RequestParam(value = "currentPage",required = false,defaultValue = "1") Integer currentPage,
                                              @RequestParam(value = "pageSize",required = false,defaultValue = "20") Integer pageSize,
                                              @RequestParam(value = "keyword",required = false) String keyword) {

        PageResult<Map<String,Object>> pageResult = productService.getProductListForAd(currentPage,pageSize,keyword);

        return ServerResponse.getSuccess("success",pageResult);
    }

    @ApiOperation("更新轮播图使用状态")
    @PostMapping(value = "/updateUseStatus")
    @ResponseBody
    public ServerResponse adStatusManage(@RequestParam(value = "id") Long id,
                                         @RequestParam(value = "isUsed") Long isUsed) {

        if(isUsed < 0 || isUsed > 1) {
            return ServerResponse.getErrorMessage("轮播状态码有误");
        }

        Boolean result = adService.updateUseStatus(id,isUsed);
        if(!result) {
            return ServerResponse.getErrorMessage("更新失败");
        }

        return ServerResponse.getSuccess("更新成功");
    }

    @ApiOperation("新增轮播广告")
    @PostMapping(value = "/create")
    @ResponseBody
    public ServerResponse createNewAd(@RequestParam(value = "productId") Long productId,
                                      @RequestParam(value = "adImgUrl") String adImgUrl,
                                      @RequestParam(value = "adProductH5Url") String adProductH5Url,
                                      @RequestParam(value = "isUsed",required = false,defaultValue = "0") Long isUsed,
                                      @RequestParam(value = "cateId",required = false,defaultValue = "0") Integer cateId) {
        if(isUsed < 0 || isUsed > 1) {
            return ServerResponse.getErrorMessage("轮播状态码有误");
        }

        if(!TextValidator.isUrl(adProductH5Url)) {
            return ServerResponse.getErrorMessage("广告url错误，请重新录入");
        }

        String adImgUri = qiniuService.getUri(adImgUrl);

        Boolean result = adService.saveProductAd(productId,adImgUri,adProductH5Url,isUsed,cateId);

        if(!result) {
            return ServerResponse.getErrorMessage("保存失败");
        }
        return ServerResponse.getSuccess("保存成功");
    }

    @ApiOperation("更新轮播信息")
    @PostMapping(value = "/update")
    @ResponseBody
    public ServerResponse updateAd(@RequestParam(value = "productId") Long productId,
                                   @RequestParam(value = "adImgUrl") String adImgUrl,
                                   @RequestParam(value = "adProductH5Url") String adProductH5Url,
                                   @RequestParam(value = "isUsed") Long isUsed,
                                   @RequestParam(value = "id") Long id,
                                   @RequestParam(value = "cateId",required = false,defaultValue = "0") Integer cateId) {

        if(isUsed < 0 || isUsed > 1) {
            return ServerResponse.getErrorMessage("轮播状态码有误");
        }

        if(!TextValidator.isUrl(adProductH5Url)) {
            return ServerResponse.getErrorMessage("广告url错误，请重新录入");
        }

        String adImgUri = qiniuService.getUri(adImgUrl);

        Boolean result = adService.updateAd(productId,adImgUri,adProductH5Url,isUsed,id,cateId);

        if(!result) {
            return ServerResponse.getErrorMessage("更新失败");
        }
        return ServerResponse.getSuccess("更新成功");
    }
}
